package com.ruoyi.web.job;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class XhsTest {
    private static final String NOTE_URL = "https://www.xiaohongshu.com/explore/68457dab0000000022028094";
    private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36";

    public static void main(String[] args) {
        try {
            Map<String, String> noteInfo = getNoteInfo();
            System.out.println("笔记信息：");
            noteInfo.forEach((k, v) -> System.out.println(k + ": " + v));
        } catch (IOException e) {
            System.err.println("抓取失败: " + e.getMessage());
        }
    }

    public static Map<String, String> getNoteInfo() throws IOException {
        Document doc = Jsoup.connect(NOTE_URL)
                .userAgent(USER_AGENT)
                .timeout(10000)
                .get();

        Map<String, String> result = new HashMap<>();

        // 提取标题（根据实际页面结构调整选择器）
        Element titleElement = doc.selectFirst("h1.title");
        if(titleElement != null) {
            result.put("title", titleElement.text());
        }

        // 提取作者
        Element authorElement = doc.selectFirst("a.author-name");
        if(authorElement != null) {
            result.put("author", authorElement.text());
        }

        // 提取点赞数
        Element likeElement = doc.selectFirst("span.like-count");
        if(likeElement != null) {
            result.put("likes", likeElement.text());
        }

        // 提取正文内容（简化示例）
        Element contentElement = doc.selectFirst("div.note-content");
        if(contentElement != null) {
            result.put("content", contentElement.text().substring(0, 50) + "...");
        }

        return result;
    }
}
